//
//  YMDTDetailsViewController.m
//  On The Load
//
//  Created by 我爱的妞 on 15/11/12.
//  Copyright © 2015年 @@叶一闽. All rights reserved.
//

#import "YMDTDetailsViewController.h"
#import "AppTools.h"
#import <UIImageView+WebCache.h>
#import "MBProgressHUD+show.h"
#import "YYMDetailsListViewController.h"

@interface YMDTDetailsViewController ()

@property (strong, nonatomic) IBOutlet UILabel *cityNameCHN;
@property (strong, nonatomic) IBOutlet UILabel *cityNameENG;
@property (strong, nonatomic) IBOutlet UIButton *foodBtn;
- (IBAction)foodAction:(UIButton *)sender;
@property (strong, nonatomic) IBOutlet UIButton *shopBtn;
- (IBAction)shopAction:(id)sender;
@property (strong, nonatomic) IBOutlet UIButton *happyBtn;
- (IBAction)happyAction:(UIButton *)sender;
@property (strong, nonatomic) IBOutlet UIButton *hotelBtn;
- (IBAction)hotelAction:(UIButton *)sender;
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UILabel *desLabel;
@property (strong, nonatomic) IBOutlet UIImageView *desImageView;
@property (strong, nonatomic) IBOutlet UIImageView *mapImageView;
- (IBAction)backAction:(id)sender;
@property (nonatomic, strong) NSDictionary *allDic;
@end

@implementation YMDTDetailsViewController

- (void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBar.hidden = YES;
    
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"DTDetalisView" owner:self options:nil] lastObject];
    
    self.shopBtn.layer.masksToBounds = YES;
    self.shopBtn.layer.cornerRadius = 25;
    
    self.happyBtn.layer.masksToBounds = YES;
    self.happyBtn.layer.cornerRadius = 25;
    
    self.foodBtn.layer.masksToBounds = YES;
    self.foodBtn.layer.cornerRadius = 25;
    
    self.hotelBtn.layer.masksToBounds = YES;
    self.hotelBtn.layer.cornerRadius = 25;
    
    self.bgScrollView.showsVerticalScrollIndicator = NO;
    
    self.bgScrollView.alpha = 0;
    
    self.view = view;
    self.view.backgroundColor = [UIColor clearColor];
    self.bgScrollView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.35];
    
    if (_image == nil) {
        self.bgImageView.backgroundColor = [UIColor whiteColor];
    }else{
        self.bgImageView.image = _image;
    }
    
    self.bgScrollView.contentSize = CGSizeMake(0, HEIGHT + 200);
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES].labelText = @"loading...";
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    
    [_desImageView addGestureRecognizer:tap];
    
    _desImageView.userInteractionEnabled = YES;
    
    [self afnGetData];
}

- (void)afnGetData{
    
    NSString *http = [NSString stringWithFormat:@"http://api.mxtrip.cn/api/area/getIndexData?cityid=%@&uid=&_tk=1446642323:2537&apiVersion=1.1&clientVersion=1.2.1&idfa=850C72B5-29E6-464B-B861-D4895E1A9641", _cityID];
    
    [AppTools getAFN:http andpara:nil andBlock:^(id result) {
        
        NSDictionary *allDic = [result objectForKey:@"data"];
        
        _allDic = allDic;
        
        NSDictionary *areaDic = [allDic objectForKey:@"area"];
        
        _cityNameCHN.text = [areaDic objectForKey:@"city_name"];
        _cityNameENG.text = [areaDic objectForKey:@"city_en_name"];
        
        NSArray *bussArray = [allDic objectForKey:@"business_place"];
        NSDictionary *bussDic = [bussArray lastObject];
        
        if (bussDic != nil) {
            _titleLabel.text = [bussDic objectForKey:@"place_name"];
            _desLabel.text = [bussDic objectForKey:@"card_brief"];
            _desImageView.alpha = 0.5;
            [_desImageView sd_setImageWithURL:[NSURL URLWithString:[bussDic objectForKey:@"pic"]] placeholderImage:nil];
        }
        
        if (_image == nil) {
            [_bgImageView sd_setImageWithURL:[NSURL URLWithString:[areaDic objectForKey:@"city_pic"]] placeholderImage:nil];
        }
        
        [_mapImageView sd_setImageWithURL:[NSURL URLWithString:[allDic objectForKey:@"map_pic"]] placeholderImage:[UIImage imageNamed:@"img_map_default_detail.jpg"]];
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        [UIView animateWithDuration:0.3 animations:^{
           
            self.bgScrollView.alpha = 1;
            
        }];
        
    } andfailBlock:^(id result) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showErrorWithText:@"加载失败"];
    }];
    
}

- (IBAction)foodAction:(UIButton *)sender {
    
    NSString *url = [NSString stringWithFormat:@"http://api.mxtrip.cn/api/area/ps?_nf=1&uid=&radius=&longitude=121.541621&from=1&latitude=38.880142&type=cy&hotelstar=&timeat=&num=10&cityid=%@&sort=default&score=&cost=&_tk=1446650125:7309&apiVersion=1.1&clientVersion=1.2.1&idfa=850C72B5-29E6-464B-B861-D4895E1A9641", _cityID];
    
    YYMDetailsListViewController *vc = [[YYMDetailsListViewController alloc] init];
    vc.url = url;
    
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (IBAction)shopAction:(id)sender {
    
    NSString *url = [NSString stringWithFormat:@"http://api.mxtrip.cn/api/area/ps?_nf=1&uid=&radius=&longitude=121.541628&from=1&latitude=38.880153&type=gw&hotelstar=&timeat=&num=10&cityid=%@&sort=default&score=&cost=&_tk=1446650181:2877&apiVersion=1.1&clientVersion=1.2.1&idfa=850C72B5-29E6-464B-B861-D4895E1A9641", _cityID];
    
    YYMDetailsListViewController *vc = [[YYMDetailsListViewController alloc] init];
    vc.url = url;
    
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (IBAction)happyAction:(UIButton *)sender {
    
    NSString *url = [NSString stringWithFormat:@"http://api.mxtrip.cn/api/area/ps?_nf=1&uid=&radius=&longitude=121.541647&from=1&latitude=38.880123&type=jd&hotelstar=&timeat=&num=10&cityid=%@&sort=default&score=&cost=&_tk=1446645502:11717&apiVersion=1.1&clientVersion=1.2.1&idfa=850C72B5-29E6-464B-B861-D4895E1A964", _cityID];
    
    YYMDetailsListViewController *vc = [[YYMDetailsListViewController alloc] init];
    vc.url = url;
    
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)hotelAction:(UIButton *)sender {
    
    NSString *url = [NSString stringWithFormat:@"http://api.mxtrip.cn/api/area/ps?_nf=1&uid=&radius=&longitude=121.541592&from=1&latitude=38.880131&type=hotel&hotelstar=&timeat=&num=10&cityid=%@&sort=default&score=&cost=&_tk=1446650242:577&apiVersion=1.1&clientVersion=1.2.1&idfa=850C72B5-29E6-464B-B861-D4895E1A9641", _cityID];
    
    YYMDetailsListViewController *vc = [[YYMDetailsListViewController alloc] init];
    vc.url = url;
    
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (IBAction)backAction:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)tapAction{
    
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    
    NSDictionary *dic = [[_allDic objectForKey:@"business_place"] lastObject];
    
    NSDictionary *subDic = [[dic objectForKey:@"links"] objectAtIndex:0];
    
    NSString *url = [subDic objectForKey:@"link_h5"];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    
    [webView loadRequest:request];
    
    BaseViewController *baseVC = [[BaseViewController alloc] init];
    
    [baseVC.view addSubview:webView];
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController pushViewController:baseVC animated:YES];
}

@end
